<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="toAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格显示商品信息 -->
      <el-table :data="goodsList" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="价格"
          prop="goods_price"
          width="120"
        ></el-table-column>
        <el-table-column
          label="数量"
          prop="goods_number"
          width="120"
        ></el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 页码视图区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
      >
      </el-pagination>
    </el-card>

    <!-- 编辑商品信息对话框 -->
    <el-dialog
      title="编辑商品信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editClosed"
    >
      <el-form :model="editForm" ref="editFormRef" :rules="editFormRules" label-width="120px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="商品简介">
          <el-input v-model="editForm.goods_introduce"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsInfo"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      /* 商品列表数组 */
      goodsList: [1],
      /* 商品总条数 */
      totalNum: 0,
      /* 控制编辑商品信息对话框的显示与隐藏 */
      editDialogVisible: false,
      /* 编辑对话框表单 */
      editForm: {
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        goods_introduce: '',
        pics: [],
        attrs: []
      },
      editFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur'}
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur'}
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur'}
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur'}
        ]
      },
      editGoodsId: ''
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('商品列表数据获取失败！')
      }
      this.goodsList = res.data.goods
      this.totalNum = res.data.total
    },
    /* 改变每页显示数据条数 */
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.getGoodsList()
    },
    /* 改变显示页数 */
    handleCurrentChange(pageNum) {
      this.queryInfo.pagenum = pageNum
      this.getGoodsList()
    },
    /* 根据商品id删除商品 */
    async removeById(id) {
      const confirmResult = await this.$confirm(
        '此操作将会永久的删除该商品，是否继续？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除该商品！')
      }

      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败！')
      }
      this.$message.success('删除商品成功！')
      this.getGoodsList()
    },
    /* 跳转到添加商品页面 */
    toAddPage() {
      this.$router.push('/goods/add')
    },
    /* 显示编辑商品信息对话框 */
    async showEditDialog(id) {
      /* this.editGoodsId = id
      const {data:res} = await this.$http.get(`goods/${id}`)
      console.log(res)
      if(res.meta.status !== 200){
        return this.$message.error(res.meta.msg)
      }
      const goods = res.data
      this.editForm.goods_name = goods.goods_name
      this.editForm.goods_price = goods.goods_price
      this.editForm.goods_number = goods.goods_number
      this.editForm.goods_weight = goods.goods_weight
      this.editForm.goods_introduce = goods.goods_introduce
      this.editForm.pics = goods.pics
      this.editForm.attrs = goods.attrs

      this.editDialogVisible = true */
      this.$message.error('接口文档错误！')
    },
    editGoodsInfo() {
      this.$refs.editFormRef.validate(async value=>{
        if(!value) {
          return this.$message.error('表单验证不通过！')
        }
        console.log(this.editForm)
        const {data:res} = await this.$http.put(`goods/${this.editGoodsId}`, this.editForm)
        console.log(res)
        if(res.meta.status !== 201) {
          return this.$message.error(res.meta.msg)
        }
        this.getGoodsList()
        this.editDialogVisible = false
      })
    },
    /* 编辑对话框关闭清空表单数据 */
    editClosed() {
      this.$refs.editFormRef.resetFields()
    }
  },
}
</script>

<style lang="less" scoped>
</style>